package com.baosight.sczx.zm.cg.service;

import java.util.HashMap;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.User;
import com.baosight.sczx.common.zm.cInterface.cd.Documents;
import com.baosight.sczx.zm.cg.domain.ZMCGNotice;


/**
 * 发送通知
 * <p>Title: ServiceZMCF15.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 严洪浩
 * @version 1.0
 * <pre>Histroy:
 *          2011-7-8 上午05:03:21  严洪浩  Create
 *</pre>
 */
public class ServiceZMCG11 extends ServiceEPBase {
	/**
	 * 初始化方法
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCF15</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-8上午05:14:44 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		
		EiInfo outInfo = this.query(inInfo);
		
		outInfo.set("userId", UserSession.getUserId());
		outInfo.set("userName", UserSession.getInSessionProperty("UserCname"));
		outInfo.set("fdTime", StaticTools.getDateTime());
		return outInfo;
	}

	/**
	 * 查询方法
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCF15</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-8上午05:14:50 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		if(inInfo.getBlock(EiConstant.queryBlock) == null){
			StaticTools.setQueryParameters(inInfo, "fdEndDate", StaticTools.getDate() + " 23:59:59", true);
		}
		else {
			String endTime = inInfo.getBlock(EiConstant.queryBlock).getCellStr(0, "fdEndDate");
			if(null != endTime && !"".equals(endTime)) {
				if(endTime.length() <= 10) {
					StaticTools.setQueryParameters(inInfo, "fdEndDate", endTime + " 23:59:59", true);
				}
			}
		}
		
//		String fdPersonId = UserSession.getUserId() == null ? "" : UserSession.getUserId().toString();//编制
//		StaticTools.setQueryParameters(inInfo, "fdPersonId", fdPersonId, true);//添加查询条件
		
		String fdPerson = UserSession.getInSessionProperty("UserCname") == null ? "" : UserSession.getInSessionProperty("UserCname").toString();//审批人
		StaticTools.setQueryParameters(inInfo, "fdPerson", fdPerson, true);//添加查询条件
		StaticTools.setQueryParameters(inInfo, "fdMiningId", UserSession.getInSessionProperty("miningCode"), true);
		EiInfo outInfo = super.query(inInfo, "ZMCGNotice.query1", new ZMCGNotice());
		outInfo.set("fdMiningId", UserSession.getInSessionProperty("miningCode"));//矿ID的默认值
//		outInfo.set("userId", UserSession.getUserId());
//		outInfo.set("userName", UserSession.getInSessionProperty("UserCname"));
//		outInfo.set("fdTime", StaticTools.getDateTime());
		
		return outInfo;
	}
	
	/**
	 * 新增方法
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCF15</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-8上午05:15:01 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 
		ZMCGNotice _dom = new ZMCGNotice();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		String fdStatus = "草拟";
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			Map<String, String>	params = new HashMap<String, String>();
	
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CG_NOTICE"));//主键
			_dom.setFdStatus(fdStatus);
			_dom.setFdPerson(UserSession.getInSessionProperty("UserCname").toString());
			_dom.setFdMiningId(UserSession.getInSessionProperty("miningCode").toString());	//矿井id
			_dom.setFdTime(StaticTools.getDateTime());
			this.dao.insert("ZMCGNotice.insert",_dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 
	}
			
//		String fdPersonId = UserSession.getUserId() == null ? "" : UserSession.getUserId().toString();//编制人ID
//		String fdPerson = User.getInstance().getNameById(fdPersonId);
//		String fdTime = StaticTools.getDateTime();//编制时间
//		String fdIsApproval = inInfo.getBlock("result").getCellStr(0, "fdIsApproval");//是否审批
//		String fdStatus = "草拟";
//		String fdApprovalId = fdIsApproval.equals("1") ? inInfo.getBlock("result").getCellStr(0, "fdApprovalId") : null;
//		String fdApproval = null;
//		if(fdApprovalId != null) {
//			fdApproval = User.getInstance().getNameById(fdApprovalId);
//		}
//		
//		String fdKeyId = TablePk.getInstance().getNextValue("T_CG_NOTICE");
//		inInfo.getBlock("result").setCell(0, "fdKeyId", fdKeyId);//主键
//		inInfo.getBlock("result").setCell(0, "fdPersonId", fdPersonId);
//		inInfo.getBlock("result").setCell(0, "fdPerson", fdPerson);
//		inInfo.getBlock("result").setCell(0, "fdTime", fdTime);
//		inInfo.getBlock("result").setCell(0, "fdStatus", fdStatus);
//		inInfo.getBlock("result").setCell(0, "fdApprovalId", fdApprovalId);
//		inInfo.getBlock("result").setCell(0, "fdApproval", fdApproval);
//		inInfo.getBlock("result").setCell(0, "fdType", "0");
//		
//		String title = "通知公告：" + inInfo.getBlock("result").getCell(0, "fdTitle").toString();
//		Documents.insertDoc(inInfo, fdKeyId, title, "通知公告", "其它", "");//添加附件
//		
//		EiInfo outinfo = super.insert(inInfo, "ZMCGNotice.insert", "result");
//		
//		return outinfo;
//	}

	/**
	 * 修改方法
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCF15</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-8上午05:15:10 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 
		ZMCGNotice _dom = new ZMCGNotice();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			if(validate(_dom)){
				_dom.setFdPerson(UserSession.getInSessionProperty("UserCname").toString());
				_dom.setFdTime(StaticTools.getDateTime());
			this.dao.update("ZMCGNotice.update",_dom);
			}else{
				out = this.query(inInfo);
				out.setStatus(-1);
				out.setMsg("只能修改草拟或者不通过的通知!"); 
				return out;
			}
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;  
	}

	
	/**
	 * 删除方法
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCF15</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-8上午05:15:28 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 
		ZMCGNotice _dom = new ZMCGNotice();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			if(validate(_dom)){
				this.dao.delete("ZMCGNotice.delete", _dom);
			}else{
				out = this.query(inInfo);
				out.setStatus(-1);
				out.setMsg("只能删除草拟或者不通过的通知!"); 
				return out;
			}
				
			}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;   	
	}		
		
		
		
		
//		//删除附件信息
//		for(int i=0; i<inInfo.getBlock("result").getRowCount(); i++)
//		{
//			Documents.deleteDoc(inInfo.getBlock("result").getCellStr(i, "fdKeyId"));
//		}
//		
//		EiInfo _temp = super.delete(inInfo, "ZMCGNotice.delete", "result");
//		
//        EiInfo outInfo = this.query(inInfo);
//        outInfo.setMsg(_temp.getMsg());
//        outInfo.setDetailMsg(_temp.getDetailMsg());
//		return outInfo;
//	}
	
	/**
	 * 生效
	 * <p>Function: release</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-8-31上午07:24:30 严洪浩
	 * </p>
	 *
	 */
	public EiInfo release(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 
		ZMCGNotice _dom = new ZMCGNotice();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			if(validate(_dom)){
			String fdIsApproval = _dom.getFdIsApproval();//是否审批
			String fdStatus = fdIsApproval.equals("1") ? "待审批" : "已发布";
			_dom.setFdStatus(fdStatus);
			this.dao.update("ZMCGNotice.release",_dom);
			}else{
				out = this.query(inInfo);
				out.setStatus(-1);
				out.setMsg("只能生效草拟或者不通过的通知!"); 
				return out;
			}
			
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;  
	}
				
	
	/**
	 * 置顶
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCF15</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-8上午05:15:28 严洪浩
	 * </p>
	 *
	 */
	public EiInfo top(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 
		ZMCGNotice _dom = new ZMCGNotice();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			this.dao.update("ZMCGNotice.modifyTop",_dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 
	}

	/**
	 * 取消置顶
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCF15</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-8上午05:15:28 严洪浩
	 * </p>
	 *
	 */
	public EiInfo cancelTop(EiInfo inInfo) {		
		EiInfo out=new EiInfo(); 
		ZMCGNotice _dom = new ZMCGNotice();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			this.dao.delete("ZMCGNotice.modifyCancelTop",_dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 
	}
	
	private boolean validate(ZMCGNotice _dom){
		  String fdStatus = _dom.getFdStatus();
		  if("草拟".equals(fdStatus) || "不通过".equals(fdStatus)){
			 return true; 
		  }else
			  return false;
		  
	  }
	
	/**
	 * 查询附件
	 * <p>Function: queryFiles</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-30上午10:38:30 张志芳
	 * </p>
	 *
	 */
	public EiInfo queryFiles(EiInfo inInfo) {
		String keyId = inInfo.getString("fdKeyId");
		return Documents.getFilesInfo("filesresult", keyId);
	}
}
